package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.service.*;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author 老韦
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2024-08-16 18:28:23
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService{

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Autowired
    LabelInfoMapper labelInfoMapper;

    @Autowired
    FacilityInfoMapper facilityInfoMapper;

    @Autowired
    FeeValueMapper feeValueMapper;

    @Autowired
    ApartmentFacilityService apartmentFacilityService;

    @Autowired
    ApartmentLabelService labelService;

    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    DistrictInfoService districtInfoService;


    @Autowired
    CityInfoService cityInfoService;

    @Autowired
    ProvinceInfoService provinceInfoService;


    @Override
    public IPage<ApartmentItemVo> pageAppartmentItemByQuery(IPage<ApartmentItemVo> iPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageAppartmentItemByQuery(iPage,queryVo);
    }

    @Override
    public ApartmentDetailVo getApparmentDetailById(Long id){
        //1.查询ApartmentInfo
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null) {
            return null;
        }

        //2.查询GraphInfo
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

        //3.查询LabelInfo
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        //4.查询FacilityInfo
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByFacilityInfoId(id);

        //5.查询FeeValue
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByFeeValueId(id);

        ApartmentDetailVo adminApartmentDetailVo = new ApartmentDetailVo();

        BeanUtils.copyProperties(apartmentInfo, adminApartmentDetailVo);
        adminApartmentDetailVo.setGraphVoList(graphVoList);
        adminApartmentDetailVo.setLabelInfoList(labelInfoList);
        adminApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        adminApartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return adminApartmentDetailVo;

    }





    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //如果传过来id说明是修改 没传说明是新增
        boolean flag = apartmentSubmitVo.getId()!=null;
        //多态引用 apartmentInfo
       ApartmentInfo apartmentInfo= apartmentSubmitVo;



        super.saveOrUpdate(apartmentSubmitVo);
        if (flag){
            //参数穿id了  先删除

            //删除公寓配套的信息
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId()));
            //删除公寓标签的id
            labelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId()));
            //删除杂费值的Id
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId()));
            //删除图片信息
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            lambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
            graphInfoService.remove(lambdaQueryWrapper);

        }
        //修改还是保存修改 都要 添加





        //公寓配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!facilityInfoIds.isEmpty()){
            ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facility : facilityInfoIds) {
                //new 一个ApartmentFacility 将新增的ID装进去
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facility);
                facilityList.add(apartmentFacility);
            }
        //立马塞到表李
            apartmentFacilityService.saveBatch(facilityList);
        }

        //公寓标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!labelIds.isEmpty()){
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
            for (Long labelid : labelIds) {
                //将要新增的id存入关系表对应的实体类
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelid);
                //放入我们新建的List后面好新增
                apartmentLabels.add(apartmentLabel);
            }
            labelService.saveBatch(apartmentLabels);
        }

        //公寓杂费值
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!feeValueIds.isEmpty()){
            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
            for (Long feeValueid : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueid);
                apartmentFeeValues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }

        //图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!graphVoList.isEmpty()){
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }




        //查出对应的区 修改 保存
        DistrictInfo byId = districtInfoService.getById(apartmentInfo.getDistrictId());
        LambdaUpdateWrapper<ApartmentInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ApartmentInfo::getId,apartmentSubmitVo.getId());
      //  lambdaUpdateWrapper.set(ApartmentInfo::getDistrictName,byId.getName());
        apartmentInfo.setDistrictName(byId.getName());
        ProvinceInfo byId2 = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        apartmentInfo.setProvinceName(byId2.getName());
        this.saveOrUpdate(apartmentInfo,lambdaUpdateWrapper);

        //查出对应的city_name
        CityInfo byId1 = cityInfoService.getById(apartmentInfo.getCityId());
        LambdaUpdateWrapper<ApartmentInfo> cityInfoLambdaUpdateWrapper =new LambdaUpdateWrapper<>();
        cityInfoLambdaUpdateWrapper.eq(ApartmentInfo::getId,apartmentSubmitVo.getCityId());
        //cityInfoLambdaUpdateWrapper.set(ApartmentInfo::getCityName,byId1.getName());
        apartmentInfo.setCityName(byId1.getName());
        this.saveOrUpdate(apartmentInfo,cityInfoLambdaUpdateWrapper);





    }
}




